/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import javax.swing.JOptionPane;

/**
 *
 * @author Anonymous
 */
public class DatVeGUI extends javax.swing.JFrame {

    /**
     * Creates new form DatVeGUI
     */
    public DatVeGUI() {
        initComponents();
        
        //gán các giá trị lấy được từ màn hình chính
        txtMaChuyen.setText(Integer.toString(MAIN.MAIN.macxcn));
        txtGioiHan.setText(Integer.toString(MAIN.MAIN.gh));
        
        //Thực hiện xác định miền giá trị cho số lượng vé muốn đặt
        if(MAIN.MAIN.gh>MAIN.MAIN.chotrong)//Nếu giới hạn lớn hơn chỗ trống
            //Miền giá trị 1->số chỗ trống
            spSoLV.setModel(new javax.swing.SpinnerNumberModel(1, 1, MAIN.MAIN.chotrong, 1));
        else
            //Ngược lại miền giá trị 1-> giới hạn
            spSoLV.setModel(new javax.swing.SpinnerNumberModel(1, 1, MAIN.MAIN.gh, 1));
        
        txtGiaTien.setText(Integer.toString(MAIN.MAIN.giatien*(int)spSoLV.getValue()));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtMaChuyen = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        btnDatVe = new java.awt.Button();
        jLabel9 = new javax.swing.JLabel();
        txtCMND = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtGioiHan = new javax.swing.JTextField();
        spSoLV = new javax.swing.JSpinner();
        txtGiaTien = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm Chuyến Xe");

        txtMaChuyen.setEnabled(false);

        jLabel7.setText("Mã Chuyến");

        btnDatVe.setLabel("Đặt Vé");
        btnDatVe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDatVeActionPerformed(evt);
            }
        });

        jLabel9.setText("Số Lượng Vé");

        jLabel10.setText("CMND");

        jLabel11.setText("Giới hạn");

        txtGioiHan.setEnabled(false);

        spSoLV.setModel(new javax.swing.SpinnerNumberModel(1, 1, MAIN.MAIN.gh, 1));
        spSoLV.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spSoLVStateChanged(evt);
            }
        });

        txtGiaTien.setEnabled(false);

        jLabel12.setText("Tổng Tiền");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9)
                    .addComponent(jLabel12))
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(spSoLV)
                                .addGap(53, 53, 53)
                                .addComponent(jLabel11)
                                .addGap(18, 18, 18)
                                .addComponent(txtGioiHan, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtMaChuyen, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(42, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnDatVe, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtGiaTien, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMaChuyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtGioiHan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(spSoLV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtGiaTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addComponent(btnDatVe, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Khi nhấp vào nút đặt vé
    private void btnDatVeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDatVeActionPerformed

            // TODO add your handling code here:
        if(ktDauVao())//Nếu đầu vào hợp lệ
        {
            //Đưa thông tin đặt vé vào dvdto
            DTO.DatVeDTO dvdto=new DTO.DatVeDTO();
            dvdto.setSoLuongVe((int)spSoLV.getValue());
            dvdto.setCMND(txtCMND.getText().toUpperCase());
            dvdto.setMaChuyen(Integer.parseInt(txtMaChuyen.getText()));
                           
            BUS.DatVeBUS dvbus=new BUS.DatVeBUS();
            if(dvbus.ThemDatVe(dvdto))//Nếu đặt vé thành công
            {
                JOptionPane.showMessageDialog(this,
                        "Đặt vé thành công!",
                        "Thông báo",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            else{
                JOptionPane.showMessageDialog(this,
                        "Đặt vé thất bại!",
                        "Thông báo lỗi",
                        JOptionPane.ERROR_MESSAGE);
            }
        }   
        else
        {
            JOptionPane.showMessageDialog(this,
                    "Bạn chưa nhập đủ thông tin hoặc thông tin không hợp lệ.",
                    "Thông báo lỗi",
                    JOptionPane.ERROR_MESSAGE);
                
        }
    }//GEN-LAST:event_btnDatVeActionPerformed

    private void spSoLVStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spSoLVStateChanged
        // TODO add your handling code here:
        txtGiaTien.setText(Integer.toString(MAIN.MAIN.giatien*(int)spSoLV.getValue()));
    }//GEN-LAST:event_spSoLVStateChanged
    
    //Hàm kiểm tra đầu vào
    private boolean ktDauVao()
    {
        boolean flag=false;
        if(!"".equals(txtCMND.getText().trim()))//Nếu cmnd đã có giá trị
             flag=true;
        return flag;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DatVeGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DatVeGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button btnDatVe;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSpinner spSoLV;
    private javax.swing.JTextField txtCMND;
    private javax.swing.JTextField txtGiaTien;
    private javax.swing.JTextField txtGioiHan;
    private javax.swing.JTextField txtMaChuyen;
    // End of variables declaration//GEN-END:variables

}
